#include <GuiConstantsEx.au3>
#include <GuiListView.au3>
#include <WindowsConstants.au3>

;basic GUI
Global $GUI = GUICreate("", 300, 450)
Global $hListView = GUICtrlCreateListView("A|B|C|D|E|F|G", 25, 25, 250, 400)
Global $hListItem

For $i = 0 To 20
    GUICtrlCreateListViewItem("A" & $i & "|B" & $i & "|C" & $i & "|D" & $i & "|E" & $i & "|F" & $i & "|G" & $i, $hListView)
Next

;Context Menu
Global $hCMenu = GUICtrlCreateContextMenu($hListView) ;add a context menu to the listview. I don't think you can add a seperate one to each item unless you write your own function.
Global $hCMenuText = GUICtrlCreateMenuItem("Get Text", $hCMenu) ;add the get text option to the menu.
Global $hCMenuPushTop = GUICtrlCreateMenuItem("Send to Top", $hCMenu) ;add the get text option to the menu.
Global $hCMenuPushBottom = GUICtrlCreateMenuItem("Send to Bottom", $hCMenu) ;add the get text option to the menu.
Global $sItemText ;this will store the text of the last right-clicked item.

GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY") ;This intercepts the Notification windows sends your GUI when you right click the listview (and many others) amd sends it to the "WM_NOTIFY" function.

GUISetState()

While 1
    Switch GUIGetMsg()
        Case -3
            Exit
        Case $hCMenuText
            _ShowText()
		 Case $hCMenuPushTop
			_PushToTop()
		 Case $hCMenuPushBottom
			_PushToBottom()
    EndSwitch
WEnd

;All Notify messages for your GUI will be send to this function.
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    #forceref $hWnd, $iMsg, $iwParam
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo

    If Not IsHWnd($hListView) Then $hWndListView = GUICtrlGetHandle($hListView)

    ;$ilParam is a pointer. This reads what's at that adress. (not sure why the name suggests its an int)
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
   
    $iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")

    Switch $hWndFrom ;Check what control triggered the msg
        Case $hWndListView ;If it was the listview...
            Switch $iCode ;Check what action triggered the msg
                Case $NM_RCLICK ;If it was a right click...
    				 MsgBox(0, "Debug", "Got right click" & @CRLF & "HWnd:" & $hWnd & @CRLF & "iMsg:" & $iMsg & @CRLF & "ilParam:" & $ilParam)
                    $tInfo = DllStructCreate($tagNMITEMACTIVATE, $ilParam) ;get the information about the item clicked.
					$hListItem = DllStructGetData($tInfo, "Index")
                    $sItemText =  _GUICtrlListView_GetItemText($hWndListView, DllStructGetData($tInfo, "Index"), DllStructGetData($tInfo, "SubItem")) ;store the item text in a global variable in case the get text option is clicked.
                    ;You could also just store the Index and SubItem values, the entire $tInfo struct, or whatever works best for you.

                    ;Uncomment the next part to get more information about your click.
                 _DebugPrint("$NM_RCLICK" & @LF & "--> hWndFrom:" & @TAB & $hWndFrom & @LF & _
                         "-->IDFrom:" & @TAB & $iIDFrom & @LF & _
                         "-->Code:" & @TAB & $iCode & @LF & _
                         "-->Index:" & @TAB & DllStructGetData($tInfo, "Index") & @LF & _
                         "-->SubItem:" & @TAB & DllStructGetData($tInfo, "SubItem") & @LF & _
                         "-->NewState:" & @TAB & DllStructGetData($tInfo, "NewState") & @LF & _
                         "-->OldState:" & @TAB & DllStructGetData($tInfo, "OldState") & @LF & _
                         "-->Changed:" & @TAB & DllStructGetData($tInfo, "Changed") & @LF & _
                         "-->ActionX:" & @TAB & DllStructGetData($tInfo, "ActionX") & @LF & _
                         "-->ActionY:" & @TAB & DllStructGetData($tInfo, "ActionY") & @LF & _
                         "-->lParam:" & @TAB & DllStructGetData($tInfo, "lParam") & @LF & _
                        "-->KeyFlags:" & @TAB & DllStructGetData($tInfo, "KeyFlags"))

            EndSwitch
        EndSwitch

    ;Returning this allows the GUI to handle the messages in the usual way once you're done. Returning anything else will block default behavior. (giving you a largely unresponsive GUI)
    Return $GUI_RUNDEFMSG
EndFunc

Func _ShowText()
    MsgBox(0,"Test",$sItemText)
EndFunc

Func _PushToTop()
    MsgBox(0,"Top",$sItemText)
EndFunc

Func _PushToBottom()
    MsgBox(0,"Bottom",$sItemText)
	MsgBox(0, "test", "Id from index: " & _GUICtrlListView_MapIndexToID($hListView, $hListItem))
	;_GUICtrlListView_GetItemCount
EndFunc

Func _DebugPrint($s_text, $line = @ScriptLineNumber)
    ConsoleWrite( _
            "!===========================================================" & @LF & _
            "+======================================================" & @LF & _
            "-->Line(" & StringFormat("%04d", $line) & "):" & @TAB & $s_text & @LF & _
            "+======================================================" & @LF)
EndFunc